<script setup lang="ts">
import type { ConsultOrderItem } from '@/types/consult'
import { OrderType, PrescriptionStatus } from '@/enums'
import dayjs from 'dayjs'
import { ref, computed } from 'vue'
import { cancelConsultOrderService, deleteOrderService } from '@/api/consult'
import { Dialog, Toast } from 'vant'
import { useShowPrescription } from '@/composables/showPrescription'
import { useRouter } from 'vue-router'
import type { Prescription } from '@/types/room'

const props = defineProps<{
  item: Prescription
}>()

// 查看处方
const { showPrescription } = useShowPrescription()

// 点击跳转详情
const router = useRouter()
const goDetail = (item: Prescription) => {
  router.push(`/user/consult/${item.id}`)
}
</script>

<template>
  <div class="consult-item">
    <div class="head van-hairline--bottom">
      <img class="img" src="@/assets/prescription.svg" />
      <p>处方</p>
      <span
        :class="{
          orange: item.status === PrescriptionStatus.NotPayment,
          green: item.status === PrescriptionStatus.Payment,
        }"
        >{{
          item.status === PrescriptionStatus.NotPayment && !item.medicineOrderId
            ? '未购买'
            : item.status === PrescriptionStatus.NotPayment && item.medicineOrderId
              ? '未支付'
              : item.status === PrescriptionStatus.Payment
                ? '已支付'
                : '已失效'
        }}</span
      >
    </div>
    <!-- 患者信息 -->
    <div class="body" @click="">
      <div class="body-row">
        <div class="body-label">患者</div>
        <div class="body-value tip">{{ item.name }}</div>
      </div>
      <div class="body-row">
        <div class="body-label">性别</div>
        <div class="body-value tip">{{ item.genderValue }}</div>
      </div>
      <div class="body-row">
        <div class="body-label">医生诊断</div>
        <div class="body-value tip">{{ item.diagnosis }}</div>
      </div>
      <div class="body-row">
        <div class="body-label">开药</div>
        <div class="body-value">
          {{ item.medicines.map((i) => `${i.name} * ${i.quantity}`).join('、') }}
        </div>
      </div>
      <div class="body-row">
        <div class="body-label">创建时间</div>
        <div class="body-value tip">{{ dayjs(item.createTime).format('YYYY-MM-DD HH:mm:ss') }}</div>
      </div>
    </div>
    <!-- 1. 未购买：去购买 -->
    <div class="foot" v-if="item.status === PrescriptionStatus.NotPayment">
      <van-button class="gray" plain size="small" round @click="showPrescription(item.id)"
        >查看原始处方
      </van-button>
      <van-button type="primary" plain size="small" round :to="`/medicine/pay?id=${item.id}`">
        去购买
      </van-button>
    </div>
    <!-- 2. 已购买：查看处方+订单详情 -->
    <div class="foot" v-if="item.status === PrescriptionStatus.Payment">
      <van-button class="gray" plain size="small" round @click="showPrescription(item.id)"
        >查看原始处方
      </van-button>
      <van-button type="primary" plain size="small" round :to="`/medicine/${item.medicineOrderId}`">
        订单详情
      </van-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.consult-item {
  border-radius: 4px;
  box-shadow: 0px 0px 22px 0px rgba(245, 245, 245, 0.1);
  background-color: #fff;
  margin-bottom: 10px;
  .head {
    display: flex;
    align-items: center;
    height: 50px;
    padding: 0 15px;
    .img {
      width: 20px;
      height: 20px;
    }
    > p {
      flex: 1;
      font-size: 15px;
      padding-left: 10px;
    }
    > span {
      color: var(--cp-tag);
      &.orange {
        color: #f2994a;
      }
      &.green {
        color: var(--cp-primary);
      }
    }
  }
  .body {
    padding: 15px 15px 8px 15px;
    .body-row {
      display: flex;
      margin-bottom: 7px;
    }
    .body-label {
      width: 62px;
      font-size: 13px;
      color: var(--cp-tip);
    }
    .body-value {
      width: 250px;
      color: var(--cp-text3);
      &.tip {
        color: var(--cp-text2);
      }
    }
  }
  .foot {
    padding: 0 15px 15px 15px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    .van-button {
      margin-left: 10px;
      padding: 0 16px;
      &.gray {
        color: var(--cp-text3);
        background-color: var(--cp-bg);
      }
    }
    .more {
      color: var(--cp-tag);
      flex: 1;
      font-size: 13px;
    }
  }
}
</style>
